# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QMouseEvent, QColor
from PySide6.QtWidgets import QFrame, QVBoxLayout, QHBoxLayout, QSizePolicy
from qfluentwidgets import BodyLabel, setFont, ToolTipFilter

from ..threads import queue_thread_manager
from ..widgets import ZoomImageLabel, TagLabel
from ...common import BookItemModel, signalBus, pluginManager


class ComicCard(QFrame):
    clicked = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.coverLabel = ZoomImageLabel(self)
        self.titleLabel = BodyLabel('漫画标题', self)
        self.contentLabel = BodyLabel('漫画描述', self)
        self.horizontalLayout = QHBoxLayout(self.coverLabel)

        # self.coverLabel.setMinimumSize(219, 292)
        self.installEventFilter(ToolTipFilter(self))
        self.setMinimumSize(150, 250)
        setFont(self.titleLabel, 16)
        setFont(self.contentLabel, 12)
        self.contentLabel.setTextColor(QColor(0, 0, 0, 200), QColor(255, 255, 255, 200))

        self.contentLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        self.titleLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self.contentLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(5)
        self.verticalLayout.addWidget(self.coverLabel, 1)
        self.verticalLayout.addWidget(self.titleLabel, 1)
        self.verticalLayout.addWidget(self.contentLabel, 1)

        self.horizontalLayout.setSpacing(5)
        self.horizontalLayout.setContentsMargins(5, 5, 5, 5)
        self.horizontalLayout.setAlignment(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignLeft)

    def setCover(self, url: str):
        queue_thread_manager.load_image(url, slot=self.coverLabel.setImage)

    def setTags(self, tags: Union[list, str]):
        if isinstance(tags, str):
            tags = tags.split(',')
        for tag in tags[:4]:
            tag_label = TagLabel(self)
            tag_label.setText(tag)
            self.horizontalLayout.addWidget(tag_label)

    def mouseReleaseEvent(self, event: QMouseEvent):
        pos = event.position().toPoint()
        # 判断鼠标是否是左键，并且鼠标松开位置是否在按钮中
        if event.button() == Qt.MouseButton.LeftButton and self.rect().contains(pos):
            self.clicked.emit()
        super().mouseReleaseEvent(event)

    def sizeHint(self):
        return self.minimumSize()

    def setModel(self, bookItemModel: BookItemModel):
        title = self.titleLabel.fontMetrics().elidedText(bookItemModel.title, Qt.TextElideMode.ElideRight, self.width())
        self.titleLabel.setText(title)
        desc = self.contentLabel.fontMetrics().elidedText(bookItemModel.desc, Qt.TextElideMode.ElideRight, self.width())
        self.contentLabel.setText(desc)
        self.setCover(bookItemModel.cover)
        self.setTags(bookItemModel.tags)
        self.setToolTip(bookItemModel.title)
        source = bookItemModel.source
        if isinstance(source, str):
            source = pluginManager.get(bookItemModel.source)
        self.clicked.connect(lambda: signalBus.infoSignal.emit(source, bookItemModel.info_url))
